<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:th="http://www.thymeleaf.org" 
	xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"
	xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
<div th:replace="header :: header"></div>
	<h1>『菜篮子』</h1>
	<div>
	<table border='1' cellspacing='0' cellpadding='5px'>
		<thead>
			<tr>
				<td>序号</td>
				<td>菜名</td>
				<td>菜系</td>
				<td>价格</td>
				<td>会员价</td>
				<td>状态</td>
				<td>描述</td>
				<td>图片</td>
				<td>操作</td>
			</tr>
		</thead>
		<tbody>		
			<tr th:each="food : ${foods}">
				<td th:text="${food.id}"></td>
				<td th:text="${food.foodName}"></td>
				<td th:text="${food.foodTypeId}"></td>
				<td th:text="${food.price}"></td>
				<td th:text="${food.mprice}"></td>
				<td th:text="${food.menuStatus}"></td>
				<td th:text="${food.remark}"></td>
				<td th:text="${food.img}"></td>
				<td> <a href="#" class="delFoodBtn" > <button>删除</button> </a></td>
			</tr>
		</tbody>
	</table>
		<h1>『修菜』</h1>
	
		<form>
			<p><label>序号：</label><input type="text" class="editid" 		name="id" /></p>
			<p><label>菜名：</label><input type="text" class="editfoodName" 	name="foodName" /></p>
			<p><label>菜系：</label><input type="text" class="editfoodTypeId" name="foodTypeId" /></p>
			<p><label>价格：</label><input type="text" class="editprice" 		name="price" /></p>
			<p><label>特价：</label><input type="text" class="editmprice"		name="mprice" /></p>
			<p><label>状态：</label><input type="text" class="editmenuStatus" name="menuStatus" /></p>
			<p><label>描述：</label><input type="text" class="editremark" 	name="remark" /></p>
			<button type="button" class="editFoodBtn">提交</button>
			<button type="reset">重置</button>
		</form>
		
	</div>
	
	<h1>『加菜』</h1>
	
		<form>
			<p><label>菜名：</label><input type="text" class="addfoodName" 	name="foodName" /></p>
			<p><label>菜系：</label><input type="text" class="addfoodTypeId" name="foodTypeId" /></p>
			<p><label>价格：</label><input type="text" class="addprice" 		name="price" /></p>
			<p><label>特价：</label><input type="text" class="addmprice" 	name="mprice" /></p>
			<p><label>状态：</label><input type="text" class="addmenuStatus" name="menuStatus" /></p>
			<p><label>描述：</label><input type="text" class="addremark" 	name="remark" /></p>
			<a><button type="button" class="addFoodBtn">提交</button></a>
			<button type="reset">重置</button>
		</form>

<div th:replace="footer :: footer"></div>

<script type="text/javascript">
      
  //删除菜品
	$('.delFoodBtn').click(function(){
		
		if (!confirm("确认要删除该菜品？")) { 
	        window.event.returnValue = false; 
	    }
		else{
			var id 	= $(this).parent().siblings().eq(0).text();
			$.ajax({
				type: 'POST',
				url: '/delFood',
				data:{
					id: id
				},
				dataType: 'json',
				success: function(data){
					//判断成功与否
					if(data.success){
						alert('所选菜品删除成功！');
						window.location.reload();
					} 
					else{
						alert('删除失败1');
					}
				},
				error: function (data) {
					alert('删除失败2');
				}
			});
		}
	});
	
$(window).load(function(){
	
	//添加菜品
	$('.addFoodBtn').click(function(){
		
		
		var foodName = $('.addfoodName').val();
		var foodTypeId = $('.addfoodTypeId').val();
		var price = $('.addprice').val();
		var mprice = $('.addmprice').val();
		var menuStatus = $('.addmenuStatus').val();
		var remark = $('.addremark').val();
		
		$.ajax({
			type: 'POST',
			url: '/addFood',
			data:{
				foodName: foodName,
				foodTypeId: foodTypeId,
				price: price,
				mprice: mprice,
				menuStatus: menuStatus,
				remark: remark
			},
			dataType: 'json',
			success: function(data){
				//判断成功与否
				if(data.success){
					alert('菜品添加成功！');
					window.location.reload();
				} 
				else{
					alert('添加失败');
				}
			},
			error: function (data) {
				alert('添加时发生错误！');
			}
		});
		
	});
	
	
	//修改菜品
	$('.editFoodBtn').click(function(){
		var id = $('.editid').val();
		var foodName = $('.editfoodName').val();
		var foodTypeId = $('.editfoodTypeId').val();
		var price = $('.editprice').val();
		var mprice = $('.editmprice').val();
		var menuStatus = $('.editmenuStatus').val();
		var remark = $('.editremark').val();
		
		$.ajax({
			type: 'POST',
			url: '/editFood',
			data:{
				id: id,
				foodName: foodName,
				foodTypeId: foodTypeId,
				price: price,
				mprice: mprice,
				menuStatus: menuStatus,
				remark: remark
			},
			dataType: 'json',
			success: function(data){
				//判断成功与否
				if(data.success){
					alert('所选菜品修改成功！');
					window.location.reload();
				} 
				else{
					alert('删除失败1');
				}
			},
			error: function (data) {
				alert('删除失败2');
			}
		});
		
	});
	
});
</script>
</body>
</html>




